#include <osgDB/ReadFile>
#include <osgViewer/Viewer> 
#include <osg/Node>

#include "OsgExTool/OSGLineTool.h"

class UseEventHandler : public osgGA::GUIEventHandler
{
public:

	virtual bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
	{
		osgViewer::Viewer* viewer = dynamic_cast<osgViewer::Viewer*>(&aa);
		if (!viewer) return false;

		switch (ea.getEventType())
		{
		case osgGA::GUIEventAdapter::KEYDOWN:
		{
												 if (ea.getKey() == 0xFF51)
												 {
													 viewer->getSceneData()->asGroup()->getChild(1)->setNodeMask(0);
													 viewer->getSceneData()->asGroup()->getChild(0)->setNodeMask(1);
												 }

												 if (ea.getKey() == 0xFF53)
												 {
													 viewer->getSceneData()->asGroup()->getChild(0)->setNodeMask(0);
													 viewer->getSceneData()->asGroup()->getChild(1)->setNodeMask(1);
												 }
		}
			break;
		case osgGA::GUIEventAdapter::PUSH:
			if (ea.getButton() == 4)
			{
				viewer->getSceneData()->asGroup()->getChild(0)->setNodeMask(0);
				viewer->getSceneData()->asGroup()->getChild(1)->setNodeMask(0);
			}
			break;
		case osgGA::GUIEventAdapter::DOUBLECLICK:
			if (ea.getButton() == 1)
			{
				viewer->getSceneData()->asGroup()->getChild(0)->setNodeMask(1);
				viewer->getSceneData()->asGroup()->getChild(1)->setNodeMask(1);
			}
			break;
		default:
			break;
		}
		return false;
	}
};
int main(int argc, char *argv[])
{
	osgViewer::Viewer viewer;
	osg::ref_ptr<osg::Group> root = new osg::Group();

	//root->addChild(osgDB::readNodeFile("cow.os g"));
	//root->addChild(osgDB::readNodeFile("glider.osg"));

	std::vector<osg::Vec3f> points;
	points.push_back(osg::Vec3f(0, 0, 0));
	points.push_back(osg::Vec3f(100, 0, 0));

	osg::ref_ptr<osg::Geometry> geom = OsgExTool::OSGLineTool::createLine(points, osg::Vec4f(1.0, 0.0, 0.0, 1.0));

	root->addChild(geom);

	viewer.setSceneData(root.get());
	viewer.addEventHandler(new UseEventHandler);
	viewer.realize();
	viewer.run();

	return 1;
}